package com.markhsiu.framework.netty3.hander;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

import com.markhsiu.framework.netty3.bean.Command;

/**
 * 对象传递，客户端代码
 * 
 */
public class ObjectClientHandler extends SimpleChannelHandler {

	private static ExecutorService pool = Executors.newFixedThreadPool(20);
	private static AtomicInteger count = new AtomicInteger(0);
	
	/**
	 * 当绑定到服务端的时候触发，给服务端发消息。
	 * 
	 */
	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
		// 向服务端发送Object信息
		for (int i = 0; i < 1; i++) {
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					sendObject(e.getChannel());
				}
			});
			
			pool.execute(thread);
		}
		
	}

	/**
	 * 发送Object
	 * 
	 */
	private void sendObject(Channel channel) {
		Command command = new Command();
		int c = count.incrementAndGet();
		command.setActionName("Hello action...");
		command.setIndex(c);
		channel.write(command);
	}

}

